﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Test.WebApi.TestOptions;
using Volo.Abp.Security.Claims;

namespace Test.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        JwtTokenOptions TokenOpt;
        public LoginController(IOptions<JwtTokenOptions> options)
        {
            TokenOpt = options.Value;
        }
        [HttpGet]
        public string GetToken()
        {
            LoginCom(out string token);
            return token;
        }

        private bool LoginCom(out string token)
        {
            var claims = new[]
          {
                new Claim(AbpClaimTypes.ClientId,"0aa36c4a-470c-4fa5-8bd7-f52b9b5ad0d5"),
                new Claim(ClaimTypes.Name,"test"),
                new Claim(ClaimTypes.NameIdentifier,"2aea4254-7489-1df4-4753-ec4c43ebee92"),
                new Claim(AbpClaimTypes.TenantId,"079316FE-738D-F340-5AFF-D15A6C6A4E7E")
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(TokenOpt.Secret));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwtToken = new JwtSecurityToken(TokenOpt.Issuer, TokenOpt.Audience, claims, expires: DateTime.Now.AddMinutes(TokenOpt.AccessExpiration), signingCredentials: credentials);

            token = new JwtSecurityTokenHandler().WriteToken(jwtToken);
            return true;
        }

    }
}
